package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.MessageBean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyLetterAdapter extends BaseAdapter{
	private Activity mContext ;
	private ArrayList<MessageBean>  list;
	private String TAG ;
	public MyLetterAdapter(Activity context,String tag){
		this.mContext = context;
		TAG = tag ;
	}
	public void setlist (ArrayList<MessageBean>  list) {
		this.list = list ;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (getCount() != 0) {
			return list.get(position-1);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null ;
		if (convertView==null) {
			convertView = RelativeLayout.inflate(mContext,
					R.layout.myletteradapter_item, null);
			holder = new ViewHolder();
			 holder.user_head =  (AsyncImageView) convertView.findViewById(R.id.user_head);
			 holder.user_name=  (TextView) convertView.findViewById(R.id.user_name);
			 holder.user_grade=  (TextView) convertView.findViewById(R.id.grade);
			 holder.user_time=  (TextView) convertView.findViewById(R.id.user_time);
			 holder.user_content=  (TextView) convertView.findViewById(R.id.user_content);
			 holder.ishasiamge=  (ImageView) convertView.findViewById(R.id.have_pic);
			 holder.personnum=  (TextView) convertView.findViewById(R.id.personnum);
			 convertView.setTag(holder);
		}else {
			holder = (ViewHolder)convertView.getTag();
		}
		if(list.get(position).getTarget_actor()!=null){
			if (list.get(position).getTarget_actor().getAvatar_file_meta()!=null) {
				String url = list.get(position).getTarget_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				holder.user_head.setUrl(TAG, url, 
						ImageOptionsFactory.getSmallPortraitOptions(mContext));
			}
			holder.user_head.setTag(list.get(position).getTarget_actor().getActor_id());
			holder.user_name.setText(list.get(position).getTarget_actor().getDisplay_name());
			holder.user_grade.setText(list.get(position).getTarget_actor().getLevel()+"");
		}
		holder.user_head.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, PersonCenterActivity.class);
				intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, (Integer)v.getTag());
				mContext.startActivity(intent);
			}
		});
		
		
		holder.user_time.setText(DateUtil.gettopicDate(list.get(position).getLast_updated_time()));
		
		if (list.get(position).getLast_message().getContent().equals("")) {
			holder.ishasiamge.setVisibility(View.VISIBLE);
			holder.user_content.setVisibility(View.GONE);
		}else {
			holder.user_content.setVisibility(View.VISIBLE);
			holder.ishasiamge.setVisibility(View.GONE);
			holder.user_content.setText(list.get(position).getLast_message().getContent());
		}
		if (list.get(position).getNew_message_count()>0) {
			holder.personnum.setVisibility(View.VISIBLE);
			holder.personnum.setText(list.get(position).getNew_message_count()+"");
		}else {
			holder.personnum.setVisibility(View.INVISIBLE);
		}
		
		return convertView;
	}
	class ViewHolder {
		AsyncImageView user_head;
		 TextView  user_name,user_time,user_grade,
		user_content,personnum;
		 ImageView ishasiamge ;
	}

}
